package cn.sdormitory.smartdor.service.impl;

import cn.sdormitory.basedata.dao.BStudentDao;
import cn.sdormitory.basedata.entity.BDormitory;
import cn.sdormitory.basedata.entity.BStudent;
import cn.sdormitory.basedata.service.BDormitoryService;
import cn.sdormitory.smartdor.entity.AdManualAttence;
import cn.sdormitory.smartdor.service.SdManualAttenceService;
import cn.sdormitory.smartdor.vo.SdAttenceStudentVo;
import cn.sdormitory.smartdor.vo.SdManualAttenceVo;
import cn.sdormitory.sys.entity.SysDictDetail;
import cn.sdormitory.sys.service.SysDictDetailService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import cn.sdormitory.smartdor.dao.SdAttenceDao;
import  cn.sdormitory.smartdor.entity.SdAttence;

import javax.annotation.Resource;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

@Slf4j
@Service("sdManualAttenceServiceImpl")
public class SdManualAttenceServiceImpl implements SdManualAttenceService {

    @Resource
    private SdAttenceDao sdAttenceDao;

    @Resource
    private SysDictDetailService sysDictDetailService;


    @Resource
    private BStudentDao bStudentDao;


    @Autowired
    private BDormitoryService bDormitoryService;

    @Override
    public List<SdManualAttenceVo> getManualAttenceList() {
        List<SdManualAttenceVo> manualAttenceVoList = new ArrayList<SdManualAttenceVo>();
        //查询所有的栋号信息
        List<SysDictDetail> buildingNoList = sysDictDetailService.selectDictDataByType(BUILDING);
        // 查询所有的楼层信息
        List<SysDictDetail> storeyList = sysDictDetailService.selectDictDataByType(STOREY);
        List<SdManualAttenceVo> child = new ArrayList<SdManualAttenceVo>();
        int k = 0 ;

        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");

        for (SysDictDetail buildingDetail : buildingNoList) {
           // System.out.println("栋号ID " + buildingDetail.getId() + " 栋号名称：" + buildingDetail.getDictLabel());
            child = new ArrayList<SdManualAttenceVo>();
            SdManualAttenceVo buildingVo = new SdManualAttenceVo();
            buildingVo.setId(buildingDetail.getId());
            buildingVo.setName(buildingDetail.getDictLabel());
            buildingVo.setValue(buildingDetail.getDictValue());
            buildingVo.setPid(0l);
            buildingVo.setType(0);
            buildingVo.setChildren(child);

            manualAttenceVoList.add(buildingVo);
            for (SysDictDetail storey : storeyList) {
                //System.out.println("楼层ID " + storey.getId() + " 楼层名称：" + storey.getDictLabel());
                k+=100;
                SdManualAttenceVo storeyVo = new SdManualAttenceVo();
                child  = new  ArrayList<SdManualAttenceVo>();
                //System.out.println("storey.getId() = "+storey.getId() +"\t  k : "+k);
                storeyVo.setId(storey.getId()+k);
                storeyVo.setName(buildingVo.getName() +"-"+storey.getDictLabel());
                storeyVo.setValue(storey.getDictValue());
                storeyVo.setPid(buildingVo.getId());
                storeyVo.setType(1);
                storeyVo.setChildren(child);
                manualAttenceVoList.add(storeyVo);

                List<BDormitory> bDormitoryList = bDormitoryService.getBDormitoryListByCol(buildingDetail.getDictValue(),storey.getDictValue());
                //System.out.println("栋号： "+buildingDetail.getId() +"\t  楼层： "+storey.getId() +"\t  宿舍数量： " +bDormitoryList.size());
                for(BDormitory dormitory : bDormitoryList){
                    SdManualAttenceVo dormitoryVo = new SdManualAttenceVo();
                    child  = new  ArrayList<SdManualAttenceVo>();
                    dormitoryVo.setBuilding_no(buildingDetail.getDictValue()); // 获取栋号ID
                    dormitoryVo.setStorey(storey.getDictValue()); // 楼层号
                    dormitoryVo.setId(dormitory.getId());
                    dormitoryVo.setName(dormitory.getDormitoryNo());
                    dormitoryVo.setValue(dormitory.getId()+"");
                    dormitoryVo.setPid(storeyVo.getId());
                    dormitoryVo.setType(2);
                    dormitoryVo.setChildren(child);
                    manualAttenceVoList.add(dormitoryVo);
                }
            }
        }
        return manualAttenceVoList;
    }

    public int addManualAttence(AdManualAttence adManualAttence){
        int count = 0;
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        try {
            String[] studentNoArray = adManualAttence.getStudentNoArray();
            Integer[] statusArray = adManualAttence.getStatusArray();
            if(studentNoArray!=null && studentNoArray.length>0) {
                for (int i = 0; i < studentNoArray.length; i++) {
                    SdAttence sdAttence = new SdAttence();

                    sdAttence.setStudentNo(studentNoArray[i]);
                    sdAttence.setAccessDate(adManualAttence.getAttenceDate());
                    sdAttence.setAttenceStatus(statusArray[i] + "");
                    sdAttence.setBuildingNo(adManualAttence.getBuilding_no());
                    sdAttence.setStorey(adManualAttence.getStorey());
                    sdAttence.setBdormitoryId(adManualAttence.getDormitoryId());
                    sdAttence.setDormitoryNo(adManualAttence.getDormitoryNo());

                    System.out.println("开始执行查询，考勤时间： "+format.format(sdAttence.getAccessDate())+" 学号： "+sdAttence.getStudentNo());
                    List<SdAttence> attenceList = sdAttenceDao.getAttenceByDateAndStudentNo(format.format(sdAttence.getAccessDate()),sdAttence.getStudentNo());
                    // 根据学号以及考勤时间判断考勤表中是否有存在的记录
                    if(attenceList==null || attenceList.size()<1) {
                        sdAttenceDao.insert(sdAttence);
                    }else{
                        // 根据学号更新考勤状态
                        //System.out.println("------- 开始执行更新考勤信息： 学号： "+sdAttence.getStudentNo()+"考勤时间： "+format.format(sdAttence.getAccessDate()) +"考勤状态： "+sdAttence.getAttenceStatus());
                        sdAttenceDao.updateAttenceStatus(sdAttence.getStudentNo(),format.format(sdAttence.getAccessDate()),sdAttence.getAttenceStatus());
                    }
                }
            }
            count = 1;
        }catch (Exception ex){
            ex.printStackTrace();
            count = 0;
        }

        return  count;
    }



    /**
     * 根据宿舍id 获取学员考勤信息
     * @param bdormitoryId
     * @return
     */
    public List<SdAttenceStudentVo> getBStudentAttenceByAccessDateAdnDormitoryId(String accessDate, Long bdormitoryId){
        try {
            SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
            List<BStudent> studentList = bStudentDao.getBStudentByDormitoryId(bdormitoryId);
            //System.out.println("开始执行查询 ： 宿舍ID ： " + bdormitoryId + "对应的学生人数： " + studentList.size());
            List<SdAttenceStudentVo> attenceStudentList = new ArrayList<>();
            for (BStudent student : studentList) {
                SdAttenceStudentVo sdAttenceStudentVo = new SdAttenceStudentVo();
                sdAttenceStudentVo.setStudentId(student.getId());
                sdAttenceStudentVo.setStudentName(student.getStudentName());
                sdAttenceStudentVo.setStudentNo(student.getStudentNo());
                sdAttenceStudentVo.setClassId(student.getClassId());
                sdAttenceStudentVo.setClassName(student.getClassName());
                sdAttenceStudentVo.setPhone(student.getPhone());
                sdAttenceStudentVo.setIdcard(student.getIdcard());
                sdAttenceStudentVo.setBuildingNo(student.getBuildingNo());
                sdAttenceStudentVo.setStorey(student.getStorey());
                sdAttenceStudentVo.setBdormitoryId(student.getBdormitoryId());
                sdAttenceStudentVo.setDormitoryNo(student.getDormitoryNo());

                List<SdAttence> attenceList = sdAttenceDao.getAttenceByDateAndStudentNo(accessDate, student.getStudentNo());
                //System.out.println("学号： " + student.getStudentNo() + "\t 考勤时间： " + accessDate + " \t 获取的 考勤集合： " + attenceList.size());
                if (attenceList != null && attenceList.size() > 0) {
                    SdAttence attence = attenceList.get(0);
                    sdAttenceStudentVo.setAccessDate(attence.getAccessDate());
                    sdAttenceStudentVo.setAttenceStatus(attence.getAttenceStatus());
                } else {
                    sdAttenceStudentVo.setAccessDate(format.parse(accessDate));
                    sdAttenceStudentVo.setAttenceStatus("2");
                }
                attenceStudentList.add(sdAttenceStudentVo);
            }
            return attenceStudentList;
        }catch (Exception ex){
            ex.printStackTrace();
        }
        return null;
    }

}
